package com.zjj.bams.client;


import com.zjj.bams.entity.Account;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.net.SocketException;

/**
 * @author zhoujunjie
 * @desc TODO
 * @date
 */
public class LoginPanel extends JPanel {

    private ATMClient atm;

    public LoginPanel(ATMClient atm) {
        this.atm = atm;
        initComponents();
    }

    //确认按钮
    private void okButtonActionPerformed(ActionEvent e) {
        //收集登录界面数据
        String idStr = textField.getText();
        String password = new String(passwordField.getPassword());

        //执行Bank中登录操作
        if(idStr!=null && !idStr.equals("")){
            Long id = Long.valueOf(idStr);

            //未添加网络编程前
            /*try {
                Account acc = atm.bank.login(id, password);
                //登陆成功
                if(acc!=null){
                    //初始化BusinessPanel界面数据
                    atm.acc = acc;
                    atm.businessPanel.initBusiness();

                    //直接跳转至BusinessPanel界面
                    CardLayout cardLayout = (CardLayout) this.getParent().getLayout();
                    cardLayout.show(this.getParent(),"businessPanel");

                    //清空登录界面数据
                    clearLogin();
                }
            } catch (LoginException | NotFoundException ex) {
                //登录失败，则弹框显示失败信息，并清空密码框
                JOptionPane.showMessageDialog(null,ex.getMessage());
                passwordField.setText("");
            }*/

            //添加网络编程后
            //1.发送指令
            atm.pw.println("login#"+id+"#"+password);
            atm.pw.flush();

            //4.读服务器传回的结果
            try {
                Object o = atm.ois.readObject();
                if(o instanceof Account){
                    //登录成功 -> 跳转业务界面，初始化业务界面
                    Account acc = (Account)o;
                    if(acc!=null){
                        JOptionPane.showMessageDialog(this , "登录成功");
                        //直接跳转至BusinessPanel界面
                        CardLayout cardLayout = (CardLayout) this.getParent().getLayout();
                        cardLayout.show(this.getParent(),"businessPanel");
                        //初始化BusinessPanel界面数据
                        atm.acc = acc;
                        atm.businessPanel.initBusiness();
                        //清空登录界面
                        clearLogin();
                    }
                }else if(o instanceof String){
                    //登录失败  ->  弹出异常信息，并清空密码框
                    JOptionPane.showMessageDialog(this , o);
                    passwordField.setText("");
                }
            } catch (SocketException ex){
                JOptionPane.showMessageDialog(this , "服务器已关闭");
                System.exit(0);
            } catch (IOException | ClassNotFoundException ex) {
                throw new RuntimeException(ex);
            }

        }else{
            JOptionPane.showMessageDialog(null,"账户不能为空！");
        }
    }

    //点击取消按钮，回到主界面
    private void cancelButtonActionPerformed(ActionEvent e) {
        CardLayout cardLayout = (CardLayout) this.getParent().getLayout();
        cardLayout.show(this.getParent(),"mainPanel");

        //清空登录界面
        clearLogin();
    }

    //清空登陆界面
    public void clearLogin(){
        textField.setText("");
        passwordField.setText("");
    }


    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        title = new JLabel();
        idLabel = new JLabel();
        pwdLabel = new JLabel();
        textField = new JTextField();
        passwordField = new JPasswordField();
        okButton = new JButton();
        cancelButton = new JButton();

        //======== this ========
        setLayout(null);

        //---- title ----
        title.setText("\u767b  \u5f55  \u754c  \u9762");
        title.setFont(new Font("Microsoft YaHei", Font.BOLD, 20));
        title.setHorizontalAlignment(SwingConstants.CENTER);
        add(title);
        title.setBounds(115, 70, 255, 40);

        //---- idLabel ----
        idLabel.setText("\u8d26      \u53f7");
        idLabel.setFont(new Font("Microsoft YaHei", Font.PLAIN, 20));
        idLabel.setHorizontalAlignment(SwingConstants.CENTER);
        add(idLabel);
        idLabel.setBounds(55, 140, 125, 32);

        //---- pwdLabel ----
        pwdLabel.setText("\u5bc6      \u7801");
        pwdLabel.setFont(new Font("Microsoft YaHei", Font.PLAIN, 20));
        pwdLabel.setHorizontalAlignment(SwingConstants.CENTER);
        add(pwdLabel);
        pwdLabel.setBounds(55, 195, 125, 32);
        add(textField);
        textField.setBounds(190, 145, 200, 30);
        add(passwordField);
        passwordField.setBounds(190, 200, 200, 30);

        //---- okButton ----
        okButton.setText("\u767b  \u5f55");
        okButton.addActionListener(e -> okButtonActionPerformed(e));
        add(okButton);
        okButton.setBounds(115, 285, 92, 30);

        //---- cancelButton ----
        cancelButton.setText("\u53d6 \u6d88");
        cancelButton.addActionListener(e -> cancelButtonActionPerformed(e));
        add(cancelButton);
        cancelButton.setBounds(270, 285, 92, 30);

        setPreferredSize(new Dimension(480, 360));
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JLabel title;
    private JLabel idLabel;
    private JLabel pwdLabel;
    private JTextField textField;
    private JPasswordField passwordField;
    private JButton okButton;
    private JButton cancelButton;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
